package com.dyna.digit.process.controller;

import com.dyna.core.exception.CustomException;
import com.dyna.core.utils.ObjectUtil;
import com.dyna.digit.process.domain.form.DigitDeclareForm;
import com.dyna.digit.process.strategy.AbstractFlowHandler;
import com.dyna.digit.process.strategy.DigitFlowFactory;
import com.dyna.domain.resp.R;
import com.dyna.enums.DigitFlowStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api("申报环节/项目申报")
@RestController
@RequestMapping("/declare")
public class DigitDeclareController {

    @ApiOperation("项目申报详情")
    @GetMapping("/detail")
    public R<Object> detail(@RequestParam("projectId") Integer projectId) {
        AbstractFlowHandler strategy = DigitFlowFactory.getStrategy(DigitFlowStatusEnum.DECLARED.getStatus());
        return R.data(strategy.detail(projectId));
    }

    @ApiOperation("项目申报新增")
    @PostMapping("/add")
    public R<Boolean> add(@Valid @RequestBody DigitDeclareForm project) {
        AbstractFlowHandler strategy = DigitFlowFactory.getStrategy(DigitFlowStatusEnum.DECLARED.getStatus());
        return R.isSuccess(strategy.add(project));
    }

    @ApiOperation("项目申报编辑")
    @PostMapping("/edit")
    public R<Boolean> edit(@Valid @RequestBody DigitDeclareForm project) {
        if (ObjectUtil.isNull(project.getProjectId())) {
            throw new CustomException("项目id不能为空");
        }
        AbstractFlowHandler strategy = DigitFlowFactory.getStrategy(DigitFlowStatusEnum.DECLARED.getStatus());
        return R.isSuccess(strategy.edit(project));
    }

}
